package com.learn.sb.reids.demo.pubsub;


import com.learn.sb.redis.config.RedisConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.Topic;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class RedisPublishDemo {

	public static void main(String[] args) throws Exception {
		ApplicationContext ctx = new AnnotationConfigApplicationContext(RedisConfig.class);
		RedisTemplate redisTemplate = ctx.getBean(RedisTemplate.class);

		RedisPublishDemo demo = new RedisPublishDemo();
		demo.testPublish(redisTemplate);
	}

	public void testPublish(RedisTemplate redisTemplate) throws Exception {
		String inStr = new String();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		Topic topic = new ChannelTopic("topicA");

		while (!"exit".equals(inStr)) {
			inStr = br.readLine().toString();
			System.out.println("inStr = " + inStr);

			// 定义监听渠道，名称为 topicA
			redisTemplate.convertAndSend("topicA", inStr);
		}
	}
}
